# 基本设置
cmake_minimum_required(VERSION 3.5)
project(weishui VERSION 1.0.0 LANGUAGES CXX)

# 编译器设置
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

# 特性开关
set(USE_MQTT OFF)

# Qt 包查找
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS 
    Core 
    Quick 
    Sql 
    SerialBus 
    SerialPort 
    VirtualKeyboard 
    QuickControls2
    OpenGL
    Concurrent
    Network
)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS 
    Core 
    Quick 
    Sql 
    SerialBus 
    SerialPort 
    VirtualKeyboard 
    QuickControls2
    OpenGL
    Concurrent
    Network
)

# 查找OpenSSL
find_package(OpenSSL REQUIRED)

# MQTT 相关设置
if(USE_MQTT)
    find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Mqtt)
    add_compile_definitions(USE_MQTT)
endif()

# 源文件收集
set(PROJECT_SOURCES
    src/main.cpp
    src/ChatService.cpp
    src/ChatService.h
    src/database/DatabaseHelper.cpp
    src/database/DatabaseHelper.h
    src/communication/modbus/ModbusHelper.cpp
    src/communication/modbus/ModbusHelper.h
    src/communication/modbus/ModbusSlave.cpp
    src/communication/modbus/ModbusSlave.h
    src/core/ModuleHelper.cpp
    src/core/ModuleHelper.h
    src/core/Strategy.cpp
    src/core/Strategy.h
    src/common/ConstData.h
    src/common/typedef.h
    src/common/utils.h
    src/common/utils.cpp
    src/config/ConfigHelper.h
    src/config/ConfigHelper.cpp
    src/config/SettingManager.h
    src/config/SettingManager.cpp
    src/core/Status.h
    src/updater/UpdateChecker.cpp
    src/updater/UpdateChecker.h
    qml/qml.qrc
)

if(USE_MQTT)
    list(APPEND PROJECT_SOURCES 
        src/communication/mqtt/MqttHandler.cpp 
        src/communication/mqtt/MqttHandler.h
    )
endif()

# 第三方库
add_subdirectory(thirdParty/qtpromise)
add_subdirectory(thirdParty/json)
add_subdirectory(modbus)

# 包含目录
include_directories(
    modbus_lib
    thirdParty/json/include
)

# 目标设置
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${PROJECT_NAME}
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
else()
    if(ANDROID)
        add_library(${PROJECT_NAME} SHARED
            ${PROJECT_SOURCES}
        )
    else()
        add_executable(${PROJECT_NAME}
            ${PROJECT_SOURCES}
        )
    endif()
endif()

# 目标包含目录
target_include_directories(${PROJECT_NAME} PRIVATE
    src/database
    src/communication/modbus
    src/communication/mqtt
    src/common
    src/config
    src/core
)

# 目标链接库
target_link_libraries(${PROJECT_NAME} PRIVATE
    Qt${QT_VERSION_MAJOR}::Core 
    Qt${QT_VERSION_MAJOR}::Quick 
    Qt${QT_VERSION_MAJOR}::Sql 
    Qt${QT_VERSION_MAJOR}::SerialBus
    Qt${QT_VERSION_MAJOR}::SerialPort
    Qt${QT_VERSION_MAJOR}::VirtualKeyboard
    Qt${QT_VERSION_MAJOR}::QuickControls2
    Qt${QT_VERSION_MAJOR}::OpenGL
    Qt${QT_VERSION_MAJOR}::Concurrent
    Qt${QT_VERSION_MAJOR}::Network
    OpenSSL::SSL
    OpenSSL::Crypto
    qtpromise
    modbus
    nlohmann_json::nlohmann_json
)

if(USE_MQTT)
    target_link_libraries(${PROJECT_NAME} PRIVATE
        Qt${QT_VERSION_MAJOR}::Mqtt
    )
endif()

# Windows 特定设置
if(WIN32)
    target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
endif()

# Qt 6 特定设置
if(QT_VERSION_MAJOR EQUAL 6)
    qt_import_qml_plugins(${PROJECT_NAME})
    qt_finalize_executable(${PROJECT_NAME})
endif()

# 安装设置
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

# 自定义命令
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CMAKE_SOURCE_DIR}/qml/style/fonts $<TARGET_FILE_DIR:${PROJECT_NAME}>/fonts
)

# Linux特定安装
if(UNIX AND NOT APPLE)
    # 安装.desktop文件
    install(FILES ${PROJECT_NAME}.desktop
        DESTINATION share/applications
    )

    # 安装图标
    install(FILES assets/${PROJECT_NAME}.png
        DESTINATION share/icons
        RENAME ${PROJECT_NAME}.png
    )

    # 安装其他资源文件
    install(DIRECTORY qml/
        DESTINATION share/${PROJECT_NAME}/qml
        FILES_MATCHING PATTERN "*.qml"
    )

    # 创建安装后的权限设置
    install(CODE "
        execute_process(
            COMMAND chmod +x \${CMAKE_INSTALL_PREFIX}/bin/${PROJECT_NAME}
        )
    ")
endif()
